from tkinter import *
from sys import exit


# 定义一个类部件
class Hello(Frame):
    def __init__(self, parent=None):
        Frame.__init__(self, parent)
        self.pack()
        self.data = 42
        # 将组件附加到self
        self.make_widgets()

    def make_widgets(self):
        widget = Button(self, text="hello按钮", command=self.message)
        widget.pack()

    def message(self):
        self.data += 1
        print("data的值发生了变化：", self.data)


# 生成一个容器组件
parent = Frame(None)
parent.pack()
# 附加Hello，而不运行
Hello(parent).pack(side=RIGHT)
# 添加按钮
Button(parent, text="父组件按钮", command=exit).pack(side=LEFT)
parent.mainloop()
